package myapps.ttclient.activity.ppt;

import myapps.ttclient.GlobalVariables;
import myapps.ttclient.R;
import myapps.ttclient.TouchActionKind;
import myapps.ttclient.activity.MouseControllerActivity;
import myapps.ttclient.app.TTClientApplication;
import myapps.ttclient.dialog.ColorPickerDialog;
import myapps.ttclient.dialog.ColorPickerDialog.OnColorChangedListener;
import myapps.ttclient.protocol.message.drawingscreen.DSChangeColorMessage;
import myapps.ttclient.protocol.message.drawingscreen.DSChangeWidthMessage;
import myapps.ttclient.protocol.message.drawingscreen.DSCircleMessage;
import myapps.ttclient.protocol.message.drawingscreen.DSClearMessage;
import myapps.ttclient.protocol.message.drawingscreen.DSClipArtMessage;
import myapps.ttclient.protocol.message.drawingscreen.DSEllipseMessage;
import myapps.ttclient.protocol.message.drawingscreen.DSFreedomMessage;
import myapps.ttclient.protocol.message.drawingscreen.DSLineMessage;
import myapps.ttclient.protocol.message.drawingscreen.DSPointerMessage;
import myapps.ttclient.protocol.message.drawingscreen.DSRectangleMessage;
import myapps.ttclient.protocol.message.drawingscreen.DSRedoMessage;
import myapps.ttclient.protocol.message.drawingscreen.DSUndoMessage;
import myapps.ttclient.protocol.message.powerpoint.PPTSlideShowClickMessage;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SlidingDrawer;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class PPTDrawingScreen implements OnTouchListener, OnClickListener, 
	OnColorChangedListener, OnSeekBarChangeListener, OnLongClickListener {

	public static int REQUEST_CODE_PICKING_POINTER_ACTIVITY = 1234;
	public static int REQUEST_CODE_PICKING_CLIPART_ACTIVITY = 4321;

	public PPTSlideShowScreenActivity slideShowActivity;

	public ImageView imageView;

	private TextView tvAction;
	private TextView tvX;
	private TextView tvY;

	private int width;

	private int height;

	private SlidingDrawer slidingDrawer;

	private Button btnFreedom;
	private Button btnLine;
	private Button btnCircle;
	private Button btnEllipse;
	private Button btnRectangle;
	private Button btnClear;
	private Button btnRedo;
	private Button btnUndo;
	private Button btnPointer;
	private Button btnSelectColor;
	private Button btnSelectWidth;
	private Button btnClick;
	private Button btnCurrentColor;
	private Button btnClipArt;

	private TTClientApplication application;

	private TextView tvCurrent;
	private TextView tvMax;
	private TextView tvMin;
	private SeekBar sbWidth;

	private int mMinValue = 1;
	private int mMaxValue = 30;
	private int mCurrentValue = 5;

	private Paint mPaint = new Paint();
	
	private Boolean b_oldSlideShowClick;
	
	private int curPointerID = 0;

	private int curClipArtID = 0;

	public PPTDrawingScreen(PPTSlideShowScreenActivity slideShowActivity) {
		this.slideShowActivity = slideShowActivity;

		this.application = (TTClientApplication) this.slideShowActivity.getApplication();

		this.imageView = this.slideShowActivity.getDrawingScreenImageView();
		this.imageView.setOnTouchListener(this);

		tvAction = (TextView) this.slideShowActivity.findViewById(R.id.tvAction);
		tvX = (TextView) this.slideShowActivity.findViewById(R.id.tvX);
		tvY = (TextView) this.slideShowActivity.findViewById(R.id.tvY);
		
		tvAction.setVisibility(View.INVISIBLE);
		tvX.setVisibility(View.INVISIBLE);
		tvY.setVisibility(View.INVISIBLE);
		
		//Hai bien nay duoc khoi tao lan luot voi hai gia tri mac dinh
		Display display = slideShowActivity.getWindowManager().getDefaultDisplay();
		Point size = new Point();
		width = display.getWidth();
		height = display.getHeight();

		//Toast.makeText(slideShowActivity, String.valueOf(width) + " " +  String.valueOf(height), Toast.LENGTH_LONG).show();

		slidingDrawer = (SlidingDrawer) this.slideShowActivity.findViewById(R.id.slidingDrawer); 

		btnFreedom = (Button) this.slideShowActivity.findViewById(R.id.btnFreedom);
		btnLine = (Button) this.slideShowActivity.findViewById(R.id.btnLine);
		btnCircle = (Button) this.slideShowActivity.findViewById(R.id.btnCircle);
		btnEllipse = (Button) this.slideShowActivity.findViewById(R.id.btnEllipse);
		btnRectangle = (Button) this.slideShowActivity.findViewById(R.id.btnRectangle);
		btnClear = (Button) this.slideShowActivity.findViewById(R.id.btnClear);
		btnRedo = (Button) this.slideShowActivity.findViewById(R.id.btnRedo);
		btnUndo = (Button) this.slideShowActivity.findViewById(R.id.btnUndo);
		btnPointer = (Button) this.slideShowActivity.findViewById(R.id.btnPointer);
		btnSelectColor = (Button) this.slideShowActivity.findViewById(R.id.btnSelectColor);
		btnSelectWidth = (Button) this.slideShowActivity.findViewById(R.id.btnSelectWidth);
		btnClick = (Button) this.slideShowActivity.findViewById(R.id.btnErase);
		btnClipArt = (Button) this.slideShowActivity.findViewById(R.id.btnClipArt);

		btnCurrentColor = (Button) this.slideShowActivity.findViewById(R.id.btnCurrentColor);

		btnFreedom.setOnClickListener(this);
		btnLine.setOnClickListener(this);
		btnCircle.setOnClickListener(this);
		btnEllipse.setOnClickListener(this);
		btnRectangle.setOnClickListener(this);
		btnClear.setOnClickListener(this);
		btnRedo.setOnClickListener(this);
		btnUndo.setOnClickListener(this);
		btnPointer.setOnClickListener(this);
		btnSelectColor.setOnClickListener(this);
		btnSelectWidth.setOnClickListener(this);
		btnClick.setOnClickListener(this);
		btnClipArt.setOnClickListener(this);

		btnPointer.setOnLongClickListener(this);
		btnClipArt.setOnLongClickListener(this);

		mPaint.setColor(Color.BLUE);
	}

	public void onResume() {
		this.btnSelectWidth.setText(String.valueOf(mCurrentValue));
		this.application.sendMessage(new DSChangeWidthMessage(mCurrentValue));

		this.sendCurrentColor();
	}

	public void onPause() {

	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int action = event.getAction();
		int currentXPosition = (int) event.getX();
		int currentYPosition = (int) event.getY();
		
		//double x = (double) currentXPosition / width;
		//double y = (double) currentYPosition / height;
		
		double x = (double) currentXPosition / v.getWidth();
		double y = (double) currentYPosition / v.getHeight();

		if(action == MotionEvent.ACTION_DOWN) {
			tvAction.setText("Down");
			tvX.setText(String.valueOf(currentXPosition));
			tvY.setText(String.valueOf(currentYPosition));

			if(!bPPTSlideShowClick) {
				this.slideShowActivity.processTouchEvent(TouchActionKind.TOUCH_DOWN, x, y);

			}
		} else if(action == MotionEvent.ACTION_MOVE) {
			tvAction.setText("Move");
			tvX.setText(String.valueOf(currentXPosition));
			tvY.setText(String.valueOf(currentYPosition));

			if(!bPPTSlideShowClick) {
				this.slideShowActivity.processTouchEvent(TouchActionKind.TOUCH_MOVE, x, y);
			}
		} else if(action == MotionEvent.ACTION_UP) {
			tvAction.setText("Up");
			tvX.setText(String.valueOf(currentXPosition));
			tvY.setText(String.valueOf(currentYPosition));

			if(!bPPTSlideShowClick) {
				this.slideShowActivity.processTouchEvent(TouchActionKind.TOUCH_UP, x, y);
			}

			if(bPPTSlideShowClick) {
				this.application.sendMessage(
						new PPTSlideShowClickMessage(GlobalVariables.getTemporaryKey(), 1, x, y));
			}
		}
		
		return true;
	}

	private Boolean bPPTSlideShowClick = false;

	@Override
	public void onClick(View v) {
		if(v == btnFreedom) {
			this.application.sendMessage(new DSFreedomMessage());
			this.slidingDrawer.animateClose();
			bPPTSlideShowClick = false;

		} else if(v == btnLine) {
			this.application.sendMessage(new DSLineMessage());
			this.slidingDrawer.animateClose();
			bPPTSlideShowClick = false;

		} else if(v == btnCircle) {
			this.application.sendMessage(new DSCircleMessage());
			this.slidingDrawer.animateClose();
			bPPTSlideShowClick = false;

		} else if(v == btnEllipse) {
			this.application.sendMessage(new DSEllipseMessage());
			this.slidingDrawer.animateClose();
			bPPTSlideShowClick = false;

		} else if(v == btnRectangle) {
			this.application.sendMessage(new DSRectangleMessage());
			this.slidingDrawer.animateClose();
			bPPTSlideShowClick = false;

		} else if(v == btnClear) {
			this.application.sendMessage(new DSClearMessage());
			this.slidingDrawer.animateClose();
			bPPTSlideShowClick = false;

		} else if(v == btnRedo){
			this.application.sendMessage(new DSRedoMessage());

		} else if(v == btnUndo) {
			this.application.sendMessage(new DSUndoMessage());

		} else if(v == btnPointer) {
			this.application.sendMessage(new DSPointerMessage(curPointerID));
			this.slidingDrawer.animateClose();
			bPPTSlideShowClick = false;

		} else if(v == btnClipArt) {
			this.application.sendMessage(new DSClipArtMessage(curClipArtID));
			this.slidingDrawer.animateClose();
			bPPTSlideShowClick = false;

		} else if(v == btnSelectColor){
			this.showColorPicker();
			bPPTSlideShowClick = false;

		} else if(v == btnSelectWidth){
			showSettingWidthDialog();
			bPPTSlideShowClick = false;

		} else if(v == btnClick){
			bPPTSlideShowClick = true;

		}
	}

	private void showSettingWidthDialog() {
		LayoutInflater factory = LayoutInflater.from(this.slideShowActivity);

		final View setWidthDialogView = factory.inflate(R.layout.set_width_dialog, null);

		tvMax = (TextView) setWidthDialogView.findViewById(R.id.max_value);
		tvMax.setText(String.valueOf(mMaxValue));

		tvMin = (TextView) setWidthDialogView.findViewById(R.id.min_value);
		tvMin.setText(String.valueOf(mMinValue));

		tvCurrent = (TextView) setWidthDialogView.findViewById(R.id.current_value);
		tvCurrent.setText(String.valueOf(mCurrentValue));

		sbWidth = (SeekBar) setWidthDialogView.findViewById(R.id.seek_bar);
		sbWidth.setMax(mMaxValue - mMinValue);
		sbWidth.setProgress(mCurrentValue - mMinValue);
		sbWidth.setOnSeekBarChangeListener(this);

		AlertDialog dialog = new AlertDialog.Builder(this.slideShowActivity)
		.setTitle("Set Width")
		.setView(setWidthDialogView)
		.setPositiveButton("Apply", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				mCurrentValue = mMinValue + sbWidth.getProgress();
				btnSelectWidth.setText(String.valueOf(mCurrentValue));
				PPTDrawingScreen.this.application.sendMessage(new DSChangeWidthMessage(mCurrentValue));
			}
		})
		.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {

			}
		})
		.create();

		dialog.show();
	}

	private void showColorPicker() {
		(new ColorPickerDialog(	this.slideShowActivity, 
				this, mPaint.getColor())).show();
	}

	@Override
	public void colorChanged(int color) {
		mPaint.setColor(color);
		sendCurrentColor();
	}

	
	public void enableTemporateSlideShowClick()	{
		b_oldSlideShowClick = bPPTSlideShowClick;
		bPPTSlideShowClick = true;
	}
	
	public void restoreSlideShowClickState() {
		bPPTSlideShowClick = b_oldSlideShowClick;
	}

	private void sendCurrentColor() {
		int color = mPaint.getColor();

		//color <-> a r g b
		int r = color << 8  >> 24;
		int g = color << 16 >> 24;
		int b = color << 24 >> 24;

		this.application.sendMessage(new DSChangeColorMessage(r, g, b));
		btnCurrentColor.setBackgroundColor(color);
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progess, boolean fromUser) {
		tvCurrent.setText(String.valueOf(mMinValue + progess));
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {

	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {

	}

	@Override
	public boolean onLongClick(View v) {
		if(v == btnPointer)	{
			Intent myIntent = new Intent(this.slideShowActivity, PPTPickingPointerActivity.class);
			Bundle myBundle1 = new Bundle();
			myBundle1.putInt ("PointerId", curPointerID);
			myIntent.putExtras(myBundle1);
			this.slideShowActivity.startActivityForResult(myIntent, REQUEST_CODE_PICKING_POINTER_ACTIVITY);

		} else if (v == btnClipArt) {
			Intent myIntent = new Intent(this.slideShowActivity, PPTPickingClipArtActivity.class);
			Bundle myBundle1 = new Bundle();
			myBundle1.putInt ("ClipArtId", curClipArtID);
			myIntent.putExtras(myBundle1);
			this.slideShowActivity.startActivityForResult(myIntent, REQUEST_CODE_PICKING_CLIPART_ACTIVITY);
		}

		return false;
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == REQUEST_CODE_PICKING_POINTER_ACTIVITY) {
			if (resultCode == Activity.RESULT_OK) {
				Bundle myBundle = data.getExtras();
				curPointerID = myBundle.getInt("PointerId");
				this.application.sendMessage(new DSPointerMessage(curPointerID));
			}
		} else if (requestCode == REQUEST_CODE_PICKING_CLIPART_ACTIVITY) {
			if (resultCode == Activity.RESULT_OK) {
				Bundle myBundle = data.getExtras();
				curClipArtID = myBundle.getInt("ClipArtId");
				this.application.sendMessage(new DSClipArtMessage(curClipArtID));
			}
		}
	}
}
